package com.ruoyi.odorservice.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.odorservice.entity.Craft;
import com.ruoyi.odorservice.entity.Material;
import com.ruoyi.odorservice.service.MaterialService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.odorservice.entity.Industry;
import com.ruoyi.odorservice.entity.Odor;
import com.ruoyi.odorservice.model.vo.SearchMaterialVo;
import com.ruoyi.odorservice.service.IndustryService;
import com.ruoyi.odorservice.service.MaterialService;
import com.ruoyi.odorservice.service.OdorService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.odorservice.constant.SearchType;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;


@RestController
@RequestMapping("/material")
@Api(tags = "物质信息")
@AllArgsConstructor
public class MaterialController {

    private MaterialService materialService;

    private OdorService odorService;

    private IndustryService industryService;

    @GetMapping("/search")
    @ApiOperation(value = "按物质气味行业id搜索",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "搜索id", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "type", value = "搜索类型", paramType = "query", dataType = "String", required = true)
    })
    public Result<?> search(Integer id, String type) {
        switch (type) {
            case SearchType.MATERIAL:{
                return materialService.searchMaterial(id);
            }
            case SearchType.ODOR: {
                return materialService.searchOdor(id);
            }
            case SearchType.INDUSTRY: {
                return materialService.searchIndustry(id);
            }
            default: {
                return Result.fail();
            }
        }
    }


    @GetMapping("/searchMaterial")
    @ApiOperation(value = "按关键词模糊查询物质、气味、行业",httpMethod = "GET",response = SearchMaterialVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "搜索关键词", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "type", value = "搜索类型", paramType = "query", dataType = "String", required = true)
    })
    public Result<?> searchMaterial(@RequestParam(value = "key") String key, @RequestParam(value = "type", defaultValue = "0") String type) {
        if(StringUtils.isEmpty(key) || StringUtils.isNull(key)){
            return Result.fail("搜索关键词不能为空");
        }
        switch (type) {
            case "0" :{
                return Result.fail("搜索类型不能为空");
            }
            case SearchType.MATERIAL:{
                List<SearchMaterialVo> searchMaterialVoList = materialService.searchMaterial(key);
                return Result.ok(searchMaterialVoList);
            }
            case SearchType.ODOR: {
                List<Odor> odorList = odorService.searchOdorList(key);
                return Result.ok(odorList);
            }
            case SearchType.INDUSTRY: {
                List<Industry> industryList = industryService.searchIndustry(key);
                return Result.ok(industryList);
            }
            default:{
                return Result.fail();
            }
        }
    }
    @GetMapping("/getAllMaterial")
    @ApiOperation(value = "获取所有物质",httpMethod = "GET")
    public Result<List<Material>> getAllMaterial() {
        return materialService.getAllMaterial();
    }
}
